#!/usr/bin/env bash

# 引入配置文件
# read config file
source ./vendor/zhenggui/php-cc/phpcc.ini

# 检查phplint的命令路径是否配置
# 如果没有配置,则使用默认的路径
# 如果配置了,则进一步检查配置的路径是否正确
# check if phplint_path is config
if [[ x"$phplint_path" = x ]]; then
    phplint_path="./vendor/bin/phplint"
else
    ${phplint_path} --version > /dev/null
    if [[ $? -ne 0 ]]; then
        echo "Command phplint error, please check it and retry~"
        exit 1
    fi
fi

# 检查phpcs的命令路径是否配置
# 如果没有配置,则使用默认的路径
# 如果配置了,则进一步检查配置的路径是否正确
# check if phpcs_path is config
if [[ x"$phpcs_path" = x ]]; then
    phpcs_path="phpcs"
else
    ${phpcs_path} --version > /dev/null
    if [[ $? -ne 0 ]]; then
        echo "Command phpcs error, please check it and retry~"
        exit 1
    fi
fi

# 获取本次提交的差异文件
a=`git status -s`

array=(${a//,/ })
for var in ${array[@]}
do
    if [[ "$var" = "M" ]] || [[ "$var" = "A" ]] || [[ "$var" = "D" ]]; then
        continue
    fi

    # check php file
    if [[ ! ${var} =~ ".php" ]]; then
        echo "Check file $var: not php file, ignoring..."
        continue
    fi

    # phplint
    if [[ ${phplint} == "true" ]]; then
        phplintResult=`${phplint_path} $var | grep error`
        if [[ x"$phplintResult" != x ]]; then
            echo "-------------------------------------"
            echo "Git commit error, get phplint errors:"
            ${phplint_path} ${var} | while read line
            do
                echo ${line}
            done
            echo "-------------------------------------"
            exit 1
        else
            echo "phplint file [$var] success !"
        fi
    fi

    # phpcs
    if [[ ${phpcs} == "true" ]]; then
        phpcsResult=`${phpcs_path} -n --standard=psr2 ${var} | grep ERROR | grep -v "0 ERRORS\|exist\|usage"`
        if [[ x"$phpcsResult" != x ]]; then
            echo "-------------------------------------"
            echo "Git commit error, get phpcs errors:"
            ${phpcs_path} -n --standard=psr2 ${var} | while read line
            do
                echo ${line}
            done
            echo "-------------------------------------"
            exit 1
        else
            echo "phpcs file [$var] success !"
        fi
    fi
done

echo "Congratulations! Code check success!"
exit 0
